package br.com.ffm.praticas.facade;

import java.sql.SQLException;
import java.util.List;

import br.com.ffm.praticas.bean.Sell;
import br.com.ffm.praticas.dao.SellDAO;

public class SellFACADE {
	
	private static SellFACADE sellFacade;
	private static SellDAO sellDAO;
	
	/**
	 * Constructor of SellFACADE
	 */
	private SellFACADE(){
		sellDAO = new SellDAO();
	}
	
	/**
	 * Method that returns an unique instance of SellFACADE
	 * @return An instance of SellFACADE
	 */
	public static SellFACADE getInstance(){
		if(sellFacade == null){
			sellFacade = new SellFACADE();
		}
		return sellFacade;
	}
	
	private boolean alreadyExist(Sell sell) throws SQLException{
		Sell s = sellDAO.getSell(sell.getId());
		if(s != null){
			return true;
		}
		return false;
	}
	
	/**
	 * Method that insert a Sell
	 * @param sell The Sell to be inserted
	 * @return If insert return true
	 * @throws SQLException
	 */
	public boolean insert(Sell sell) throws SQLException{
		if(sell != null && !alreadyExist(sell)){
			sellDAO.insert(sell);
			return true;
		}
		return false;
	}
	
	/**
	 * Method that delete a Sell
	 * @param sell The sell to be deleted
	 * @return If delete return true
	 * @throws SQLException
	 */
	public boolean delete(Sell sell) throws SQLException{
		if(sell != null && alreadyExist(sell)){
			sellDAO.delete(sell);
			return true;
		}
		return false;
	}
	
	/**
	 * Method that find and return a Sell
	 * @param id The id of the sell to be found
	 * @return If found return the sell
	 * @throws SQLException
	 */
	public Sell getSell(int id) throws SQLException{
		return sellDAO.getSell(id);
	}
	
	/**
	 * Method that returns all inserted Sells
	 * @return All sells
	 * @throws SQLException
	 */
	public List<Sell> getSells() throws SQLException{
		return sellDAO.getSells();
	}
	
	/**
	 * Method that update a Sell
	 * @param sell The sell to be updated
	 * @return If update return true
	 * @throws SQLException
	 */
	public boolean update(Sell sell) throws SQLException{
		if(sell != null && alreadyExist(sell)){
			sellDAO.update(sell);
			return true;
		}
		return false;
	}
	
	/**
	 * Method that returns the number of Sells
	 * @return The number of sells
	 * @throws SQLException
	 */
	public int count() throws SQLException{
		return sellDAO.getSells().size();
	}

}
